﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.Utils;
using mendian;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.标签打印
{
    public partial class Form订单贴 : DevExpress.XtraEditors.XtraForm
    {
        //运维口返回的DTO
        private OrderCommitDTO返回 OrderCommitDTO返回 = new OrderCommitDTO返回();
        public Form订单贴()
        {
            InitializeComponent();
            if (!File.Exists(@"tag\order_material.btw"))
            {
                FileUtil.DownloadPicture(URL.图片url + URL.打印模板路径 + "order_material.btw", @"tag\" + "order_material.btw", -1);
            }
            this.ActiveControl = this.textBox生产批号;
        }

        private void textBox生产批号_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                if (String.IsNullOrWhiteSpace(this.textBox生产批号.Text))
                {
                    MessageBox.Show("请扫描生产批号");
                    return;
                }
                try
                {
                    this.OrderCommitDTO返回 = FunctionHelper.JsonDeserialization<OrderCommitDTO返回>(
                        RestCall.httpGetMethod(URL.接口url + "/pmc/getOrderCacheByOrderId?batchNo=" + this.textBox生产批号.Text.Trim()));
                }
                catch
                {
                    MessageBox.Show("无法查到订单");
                    return;
                }
                Engine btEngine = new Engine();
                btEngine.Start();
                LabelFormatDocument btFormat = btEngine.Documents.Open(System.AppDomain.CurrentDomain.BaseDirectory + @"tag\order_material.btw");
                //指定打印机名 
                //btFormat.PrintSetup.PrinterName = "HPRT HLP106S-UE";
                //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
                btFormat.PrintSetup.PrinterName = "POSTEK G-3106";
                //btFormat.PrintSetup.PrinterName = "迅捷PDF虚拟打印机";
                //btFormat.PrintSetup.PrinterName = "PDFCreator";
                //打印份数                   
                btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
                //改变标签打印数份连载 
                btFormat.PrintSetup.NumberOfSerializedLabels = 1;
                //对BTW模版相应字段进行赋值 

                btFormat.SubStrings["orderNo"].Value = "订单号:" + this.OrderCommitDTO返回.orderNo;
                btFormat.SubStrings["materialCode"].Value = this.OrderCommitDTO返回.materialCode;
                btFormat.SubStrings["batchNo"].Value = this.textBox生产批号.Text.Trim();

                Messages messages;
                int waitout = 10000; // 10秒 超时 
                Seagull.BarTender.Print.Result nResult1 = btFormat.Print("订单贴"+this.textBox生产批号.Text, waitout, out messages);
                btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;
                //不保存对打开模板的修改 
                btFormat.Close(SaveOptions.DoNotSaveChanges);
                //结束打印引擎                  
                btEngine.Stop();
                this.textBox生产批号.Clear();
                this.ActiveControl = this.textBox生产批号;
            }
        }
    }
}
